package com.client.elements.canvas;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.client.elements.canvas.tools.Kreis;

public class Kreisspiel {
	static Canvas canvas_main;
	static Canvas canvas_unten;
	static Kreis kreis;

	static Context2d context_main;
	static Context2d context_unten;

	static int mouseX = 170;
	static int balkenlange = 100;

	final static Button btn = new Button("Start");

	public static Widget kreis_demo() {

		FlowPanel main = new FlowPanel();

		canvas_main = Canvas.createIfSupported();
		canvas_unten = Canvas.createIfSupported();

		canvas_main.setCoordinateSpaceWidth(400);
		canvas_main.setCoordinateSpaceHeight(400);
		canvas_main.addStyleName("border");

		canvas_unten.setCoordinateSpaceWidth(400);
		canvas_unten.setCoordinateSpaceHeight(50);

		context_main = canvas_main.getContext2d();
		context_unten = canvas_unten.getContext2d();


		timer(true);
		setHandlers();

		btn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (btn.getText().equals("Start")) {
					kreis = new Kreis(15, 10, 100, 8,5);
					timer(false);
					btn.setText("Stop");
				}else if (btn.getText().equals("Stop")) {
					timer(true);
					btn.setText("Start");
				}
			}
		});

		main.add(canvas_main);
		main.add(canvas_unten);
		main.add(btn);

		return main;
	}

	final static Timer timer = new Timer() {
		@Override
		public void run() {
			context_unten.clearRect(0, 0, 400, 20);
			context_unten.fillRect(mouseX - balkenlange / 2, 0, balkenlange, 20);
			context_main.clearRect(0, 0, 400, 400);

			kreis.update(context_main);

			if ((kreis.getPosition_Y()  + 15 >= 400)) {
				if ((kreis.getPosition_X() > mouseX + balkenlange / 2) || (kreis.getPosition_X() < mouseX - balkenlange / 2)) {
					Window.alert("Kugel verfehlt.");
					timer(true);
					btn.setText("Start");
				}
			}
		}
	};

	private static void timer(boolean stop) {
		if (stop == false) {
			timer.scheduleRepeating(25);		
		}else {
			timer.cancel();
		}
	}

	private static void setHandlers() {
		canvas_unten.addMouseMoveHandler(new MouseMoveHandler() {
			public void onMouseMove(MouseMoveEvent event) {
				mouseX = event.getRelativeX(canvas_unten.getElement());
			}
		});

		canvas_main.addMouseMoveHandler(new MouseMoveHandler() {
			public void onMouseMove(MouseMoveEvent event) {
				mouseX = event.getRelativeX(canvas_unten.getElement());
			}
		});
	}
}